//
//  LTSendMessageHelper.m
//  LTIM
//
//  Created by 俊松何 on 2017/4/15.
//  Copyright © 2017年 俊松何. All rights reserved.
//

#import "LTSendMessageHelper.h"
#import "NetworkService.h"
#import "LTMessageResponseModel.h"
#import "LTRecvMessageModel.h"
#import "LTLoginUserModel.h"
#import "LTDBHelper.h"
#import "LTMessageApplyBody.h"
static LTSendMessageHelper *helper;

static NSString *lthttpSendUri = @"/message/send";
static NSString *lthttpRecvUri = @"/message/clientReceive";
static NSString *lthttpRegisterUri = @"lt";

@interface LTSendMessageHelper()
@property(nonatomic,assign) ChannelType linkType;


@end



@implementation LTSendMessageHelper


+(instancetype)helper
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[LTSendMessageHelper alloc] init];
        helper.linkType = ChannelType_All;
        
    });
    return helper;
}

-(void)sendRegisterMessage
{
    //注册
    CGITask *sendMsgCGI = [[CGITask alloc] initAll:ChannelType_LongConn AndCmdId:MARS_CMDID_REGISTER AndCGIUri:lthttpRegisterUri AndHost:_hostIp];
    LTLoginUserModel *loginModel = [[LTLoginUserModel alloc] init];
    
    loginModel.LoginComplentBlock =  ^(LTUser *user,LTError *error)
    {
        
    };
    loginModel.user = [LTClient sharedClient].currentUser;
    [[NetworkService sharedInstance] startTask:sendMsgCGI ForData:loginModel];


}


-(void)messageCallBackMessageID:(NSString *)messageId
                       chattype:(LTChatType)type
             originTargetUserId:(NSString *)originTargetUserId;
{
    
    LTMessageResponseModel *responsemodel = [[LTMessageResponseModel alloc] initWithCode:0 cmdId:MARS_CMDID_CLIENT_GET_MSG chatType:type messageId:messageId originTargetUserId:originTargetUserId];
    LTRecvMessageModel *callMessage = [[LTRecvMessageModel alloc] initWithResponseModel:responsemodel];
    CGITask *sendMsgCGI = [[CGITask alloc] initAll:_linkType AndCmdId:MARS_CMDID_CLIENT_GET_MSG AndCGIUri:lthttpRecvUri AndHost:_hostIp];
    [[NetworkService sharedInstance] startTask:sendMsgCGI ForData:callMessage];
    

}


-(void)sendMessage:(LTMessage *)message
        completion:(void(^)(LTMessage *message,LTError *error))completionBlock
{
    [self sendMessage:message cmdID:MARS_CMDID_SEND_MSG completion:completionBlock];
}

-(void)sendCmdMessage:(LTMessage *)message
           completion:(void(^)(LTMessage *message,LTError *error))completionBlock
{
    [self sendMessage:message cmdID:MARS_CMDID_SEND_CMDMSG completion:completionBlock];

}




//发送添加朋友申请
-(void)sendAddFriendMessage:(LTUser *)user
                    message:(NSString *)message
                 completion:(void(^)(LTUser *ltuser, LTError *ltrror))CompletionBlock
{
    
    LTMessageApplyBody *body = [[LTMessageApplyBody alloc] initWithsinceUser:[LTClient sharedClient].currentUser targetUser:user reason:message status:LTApplyStateing type:LTApplyTypePrivate];
    LTMessage *ltmessage = [[LTMessage alloc] initWithConversationID:user.userId
                                                        fromUserId:[LTClient sharedClient].currentUserId
                                                          toUserId:user.userId
                                                          fromUser:[LTClient sharedClient].currentUser
                                                            toUser:user
                                                              type:LTMessageTypeText
                                                              body:body
                                                          chatType:LTChatTypeChat ext:nil];
    [self sendMessage:ltmessage cmdID:MARS_CMDID_FRIEND_APPLY completion:^(LTMessage *message, LTError *error) {
        if(error == nil)
        {
//            [[LTDBHelper sharedbhelper] addApplyMessage:body];
        }
        CompletionBlock(user,error);
    }];

}
//发送删除朋友申请
-(void)sendDeletFriendMessage:(LTUser *)user
                   completion:(void(^)(LTUser *ltuser, LTError *ltrror))CompletionBlock
{

    LTMessageCmdBody *body = [[LTMessageCmdBody alloc] initWithType:LTMessageCmdTypeDeletefriend data:user.userId];
    
    LTMessage *ltmessage = [[LTMessage alloc] initWithConversationID:user.userId
                                                          fromUserId:[LTClient sharedClient].currentUserId
                                                            toUserId:user.userId
                                                            fromUser:[LTClient sharedClient].currentUser
                                                              toUser:user
                                                                type:LTMessageTypeCMD
                                                                body:body
                                                            chatType:LTChatTypeChat ext:nil];
    [self sendMessage:ltmessage cmdID:MARS_CMDID_SEND_CMDMSG completion:^(LTMessage *message, LTError *error) {
        CompletionBlock(user,error);
    }];



}


//同意朋友加好友申请
-(void)sendAgreeFriendMessage:(LTUser *)user
                    applyBody:(LTMessageApplyBody *)body
                   completion:(void(^)(LTUser *ltuser, LTError *ltrror))CompletionBlock
{
    body.status = LTApplyStateSuccess;
    LTMessage *ltmessage = [[LTMessage alloc] initWithConversationID:user.userId
                                                          fromUserId:[LTClient sharedClient].currentUserId
                                                            toUserId:user.userId
                                                            fromUser:[LTClient sharedClient].currentUser
                                                              toUser:user
                                                                type:LTMessageTypeAgreeApply
                                                                body:body
                                                            chatType:LTChatTypeChat ext:nil];
    [self sendMessage:ltmessage cmdID:MARS_CMDID_FRIEND_APPLY_AGREE completion:^(LTMessage *message, LTError *error) {
        if(error == nil)
        {
            [[LTDBHelper sharedbhelper] AddUser:user];
            [[LTDBHelper sharedbhelper] addApplyMessage:body];
        }
        CompletionBlock(user,error);
    }];

}
//拒绝加好友申请
-(void)sendrefuseFriendMessage:(LTUser *)user
                       applyBody:(LTMessageApplyBody *)body
                    completion:(void(^)(LTUser *ltuser, LTError *ltrror))CompletionBlock
{
    
    body.status = LTApplyStateRefuse;
    LTMessage *ltmessage = [[LTMessage alloc] initWithConversationID:user.userId
                                                          fromUserId:[LTClient sharedClient].currentUserId
                                                            toUserId:user.userId
                                                            fromUser:[LTClient sharedClient].currentUser
                                                              toUser:user
                                                                type:LTMessageTypeText
                                                                body:body
                                                            chatType:LTChatTypeChat ext:nil];
    [self sendMessage:ltmessage cmdID:MARS_CMDID_FRIEND_APPLY_REFUSE completion:^(LTMessage *message, LTError *error) {
        if(error == nil)
        {
            [[LTDBHelper sharedbhelper] addApplyMessage:body];
        }
        CompletionBlock(user,error);
    }];
}







//最终的长链接发送消息方法
-(void)sendMessage:(LTMessage *)message
              cmdID:(MARS_CMDID_TYPE)cmdtype
         completion:(void(^)(LTMessage *message,LTError *error))completionBlock
{
    
    CGITask *sendMsgCGI = [[CGITask alloc] initAll:_linkType AndCmdId:cmdtype AndCGIUri:lthttpSendUri AndHost:_hostIp];
    LTSendMessageModel *sendMessageModel = [[LTSendMessageModel alloc] init];
    sendMessageModel.message = message;
    sendMessageModel.sendComplentBlock =  ^(LTMessage *message,LTError *error,LTMessageCallBackCodeType backcodeType)
    {
        
        if(cmdtype == MARS_CMDID_SEND_MSG && message.messageType != LTMessageTypeCMD && message.chatType  != LTChatTypeChatRoom){
            [[LTDBHelper sharedbhelper] updateMessage:message completion:nil];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
        completionBlock(message,error);
    });
        
    };
    [[NetworkService sharedInstance] startTask:sendMsgCGI ForData:sendMessageModel];
}


@end
